package com.myc.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.myc.train.business.domain.TrainCarriage;
import com.myc.train.business.enums.SeatColEnum;
import com.myc.train.business.enums.SeatTypeEnum;
import com.myc.train.common.resp.PageResp;
import com.myc.train.common.utils.SnowUtil;
import com.myc.train.business.domain.TrainSeat;
import com.myc.train.business.domain.TrainSeatExample;
import com.myc.train.business.mapper.TrainSeatMapper;
import com.myc.train.business.req.TrainSeatQueryReq;
import com.myc.train.business.req.TrainSeatSaveReq;
import com.myc.train.business.resp.TrainSeatQueryResp;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TrainSeatService {

    @Resource
    private TrainSeatMapper trainSeatMapper;

    @Resource
    private TrainCarriageService trainCarriageService;

    public void save(TrainSeatSaveReq req){
        DateTime now = DateTime.now();
        TrainSeat trainSeat = BeanUtil.copyProperties(req, TrainSeat.class);

        if(ObjectUtil.isNull(trainSeat.getId())){
            trainSeat.setId(SnowUtil.getSnowflakeNextId());
            trainSeat.setCreateTime(now);
            trainSeat.setUpdateTime(now);
            trainSeatMapper.insert(trainSeat);
        }else{
            trainSeat.setUpdateTime(now);
            trainSeatMapper.updateByPrimaryKey(trainSeat);
        }
    }

    public PageResp<TrainSeatQueryResp> queryList(TrainSeatQueryReq req){
       //定义查询条件
        TrainSeatExample trainSeatExample = new TrainSeatExample();
        //根据id倒序排序
        trainSeatExample.setOrderByClause("train_code asc , carriage_index asc , carriage_seat_index asc");
        //创建查询对象
        TrainSeatExample.Criteria criteria = trainSeatExample.createCriteria();
        //不为空则对其新增查询条件
        if(ObjectUtil.isNotEmpty(req.getTrainCode())){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }

        //新增分页信息
        PageHelper.startPage(req.getPage(),req.getSize());
        //根据条件查询结果
        List<TrainSeat> trainSeats = trainSeatMapper.selectByExample(trainSeatExample);
        //new一个PageInfo并将结果放进来
        PageInfo<TrainSeat> trainSeatPageInfo = new PageInfo<>(trainSeats);
        //类拷贝
        List<TrainSeatQueryResp> queryResps = BeanUtil.copyToList(trainSeats, TrainSeatQueryResp.class);
        //new一个PageResp
        PageResp<TrainSeatQueryResp> resp= new PageResp<>();
        //放数据
        resp.setTotal(trainSeatPageInfo.getTotal());
        resp.setList(queryResps);


        return resp;
    }

    public void delete(Long id){
        trainSeatMapper.deleteByPrimaryKey(id);
    }

    //生成座位
    @Transactional
    public void genTrainSeat(String trainCode){
        DateTime now = DateTime.now();
        //1.删除原有座位
        TrainSeatExample trainSeatExample = new TrainSeatExample();
        TrainSeatExample.Criteria criteria = trainSeatExample.createCriteria();
        criteria.andTrainCodeEqualTo(trainCode);
        trainSeatMapper.deleteByExample(trainSeatExample);
        //2.获取当前车次下所有的车厢
        List<TrainCarriage> trainCarriages = trainCarriageService.selectCarriage(trainCode);
        //3.循环生成座位
        for(TrainCarriage trainCarriage: trainCarriages){
            Integer CarriageSeatIndex = 1;
            //3.1 获取车厢的行数和座位类型
            Integer rowCount = trainCarriage.getRowCount();
            String seatType = trainCarriage.getSeatType();
            //3.2 根据座位类型来生成该类型有多少列
            List<SeatColEnum> colEnumList = SeatColEnum.getColsByType(seatType);
            //3.3 循环行数
            for (int row = 1; row <= rowCount; row++) {
                //3.3.1 循环列数
                for(SeatColEnum seatColEnum: colEnumList){
                    //3.3.2 构造列数
                    TrainSeat trainSeat = new TrainSeat();
                    trainSeat.setId(SnowUtil.getSnowflakeNextId());
                    trainSeat.setTrainCode(trainCode);
                    trainSeat.setCarriageIndex(trainCarriage.getIndex());
                    trainSeat.setRow(StrUtil.fillBefore(String.valueOf(row), '0', 2));
                    trainSeat.setCol(seatColEnum.getCode());
                    trainSeat.setSeatType(seatType);
                    trainSeat.setCarriageSeatIndex(CarriageSeatIndex++);
                    trainSeat.setCreateTime(now);
                    trainSeat.setUpdateTime(now);
                    trainSeatMapper.insert(trainSeat);
                }
            }
        }
    }

    //根据车次查询车座列表
    public List<TrainSeat> selectByTrainCode(String trainCode){
        TrainSeatExample trainSeatExample = new TrainSeatExample();
        trainSeatExample.setOrderByClause("carriage_index asc , carriage_seat_index asc");
        trainSeatExample.createCriteria().andTrainCodeEqualTo(trainCode);
        return trainSeatMapper.selectByExample(trainSeatExample);
    }
}
